package com.le.tester.javaThink.fifth.chapter24_ConcurrentProgramming;

import java.util.Arrays;
import java.util.concurrent.CompletableFuture;

/**
 * createTime：2021/8/23 14:59
 * description：DiningPhilosophers 筷子是不是被释放
 */
public class DiningPhilosophers {
    private StickHolder[] sticks;
    private Philosopher[] philosophers;

    public DiningPhilosophers(int n) {
        sticks = new StickHolder[n];
        Arrays.setAll(sticks, i -> new StickHolder());
        philosophers = new Philosopher[n];

        Arrays.setAll(philosophers, i -> new Philosopher(i, sticks[i], sticks[(i + 1) % n]));

        Arrays.stream(philosophers)
                .forEach(CompletableFuture::runAsync);
    }

    public static void main(String[] args) {
        new DiningPhilosophers(5);

        new Nap(3, "Shutdown");
    }


}
